'use strict';

const Controller = require('egg').Controller;

class SmsController extends Controller {
    /**
     * 获取用户列表
     */
    async sendCode() {
        const { ctx, config, app } = this;
        let { phone, verkey, vercode } = ctx.request.body;
        vercode = vercode || "vercode";
        let vercode_set = await ctx.service.redis.get(verkey) || "vercode_set";
        if ((vercode.toUpperCase()) != (vercode_set.toUpperCase())) {
            await app.utils.error.throw("图形验证码错误", -99);
        }
        //清空
        await ctx.service.redis.del(verkey);
        var smscode = "";
        for (var i = 0; i < 6; i++) {
            smscode += Math.floor(Math.random() * 10);
        }
        let ret = await ctx.service.sms.sendCode(phone, smscode);
        let smscodekey = await app.utils.uuid.v1();
        await ctx.service.redis.set(smscodekey, smscode, 300);
        ctx.body = {
            code: 200,
            msg: "发送成功",
            data: {
                key: smscodekey
            }
        };
    }

    /**
     * 添加用户
     */
    async add() {
        const { ctx } = this;
        console.log("提交的数据", ctx.request.body);
        let query = { limit: 0, offset: 10 };
        // let data = await ctx.model.jws_user.findAll(query);
        ctx.body = {
            code: 200,
            msg: "成功",
            data: {},
        };
    }
}

module.exports = SmsController;